package com.lzwork.gpt.db.entity;

import com.lzwork.utility.collection.CollUtility;
import com.lzwork.utility.data.NumberParse;
import com.lzwork.utility.pair.IntTuple;
import com.lzwork.utility.pair.Tuple;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * @author LiZheng
 * @date 2023年12月21日 上午10:45:14
 */

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class InfoEntity
{
	int id;
	int mainversion;
	int subversion;
	int release;

	@Override
	public String toString()
	{
		return mainversion + "." + subversion + "." + release;
	}

	public void setVersion(IntTuple version)
	{
		this.mainversion = version.first();
		this.subversion = version.second();
		this.release = version.third();
	}

	public static IntTuple strToVersion(String verStr)
	{
		String[] parts = verStr.split(".");

		int v = NumberParse.objToInt(CollUtility.arrayGet(parts, 0), 0);
		int sv = NumberParse.objToInt(CollUtility.arrayGet(parts, 1), 0);
		int rl = NumberParse.objToInt(CollUtility.arrayGet(parts, 2), 0);

		return new IntTuple(v, sv, rl);
	}

	public boolean matchVersion(String verStr)
	{
		Tuple<Integer, Integer, Integer> vTuple = strToVersion(verStr);
		return matchVersion(vTuple.first(), vTuple.second(), vTuple.third());
	}

	public boolean matchVersion(IntTuple vTuple)
	{
		return matchVersion(vTuple.first(), vTuple.second(), vTuple.third());
	}

	public boolean matchVersion(InfoEntity target)
	{
		return matchVersion(target.mainversion, target.subversion, target.release);
	}

	public boolean matchVersion(int version, int subversion, int release)
	{
		if (this.mainversion >= version)
		{
			if (this.subversion >= subversion)
			{
				if (this.release >= release)
					return true;
			}
		}

		return false;
	}

}
